#!/bin/sh
### BEGIN INIT INFO
# Provides:         ckb-next
# Required-Start:   $local_fs $remote_fs
# Required-Stop:    $local_fs $remote_fs
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:        Start and stop the corsair keyboards and mice daemon
### END INIT INFO
#
# Copyright 2017-2018 ckb-next Development Team <ckb-next@googlegroups.com>
# Distributed under the terms of the GNU General Public License v2

NAME="ckb-next-daemon"
DESC="Corsair keyboards and mice"
PIDFILE="/dev/input/ckb0/pid" # DO NOT CHANGE
DAEMON="@CMAKE_INSTALL_PREFIX@/bin/ckb-next-daemon"

# Exit if daemon not installed
[ -x "$DAEMON" ] || exit 0

. /lib/lsb/init-functions


do_start () {
    log_daemon_msg "Starting $DESC" "$NAME"
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background
    log_end_msg $?
}

do_stop () {
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --quiet --retry="-QUIT/5/TERM/5/KILL/5" --pidfile $PIDFILE --name $NAME
    log_end_msg $?
}

case "$1" in
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        do_start
        ;;
    reload)
        log_daemon_msg "Reloading $NAME" "$NAME"
        start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
        log_end_msg $?
        ;;
    status)
        status_of_proc -p $PID $DAEMON $NAME
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|status}" >&2
        exit 1
esac

exit 0
